#include "ubooksdatabase.h"
#include "udatabase.h"
#include <QSqlDatabase>
#include <vector>  
#include <json/json.h>
#include <iostream>
UBooksDatabase::UBooksDatabase()
	:m_BooksDatabase(UDataBase::BooksDatabase())//创建数据库表
{
	QSqlQuery(m_BooksDatabase).exec(
		"CREATE		TABLE IF NOT  EXISTS BOOKS_INFORMATION("
		"ISBN		TEXT NOT NULL PRIMARY KEY,"
		"TITLE		TEXT NOT NULL,"
		"AUTHOR		TEXT NOT NULL,"
		"PRESS		TEXT NOT NULL,"
		"DATE		TEXT NOT NULL,"
		"IMAGE		TEXT NOT NULL);"
	); //创建书籍信息表
	//m_BooksDatabase.close();
}

UBooksDatabase* UBooksDatabase::Instance()
{
	static UBooksDatabase Instance;
	return &Instance;
}

UBooksDatabase::~UBooksDatabase()
{}

void UBooksDatabase::addBook(const QString& ISBN, const QString& title, const QString& author, const QString& press, const QString& date, const QString& image)
{
	QSqlQuery query(m_BooksDatabase);
	query.exec(QString("SELECT ISBN FROM BOOKS_INFORMATION WHERE ISBN='%1';")
		.arg(ISBN)); //查询是否已经存在
	if (query.next())return;

	query.exec(QString("INSERT INTO BOOKS_INFORMATION VALUES ('%1','%2', '%3', '%4', '%5','%6');")
		.arg(ISBN)
		.arg(title)
		.arg(author)
		.arg(press)	
		.arg(date)
		.arg(image));							//插入默认书籍信息;
	return;
}

bool UBooksDatabase::queryBookByIsbn(const QString& ISBN)
{
	QSqlQuery query(m_BooksDatabase);
	query.exec(QString("SELECT ISBN FROM BOOKS_INFORMATION WHERE ISBN='%1';").arg(ISBN)); //查询ISBN是否已经存在 true为不存在，false为存在
	return query.next() ? false : true;
}

QString UBooksDatabase::queryBook(const QString& title, const QString& ISBN, const QString& author)
{
	QList<UBooksInfo> uBooksInfoList;
	qDebug() << "---------------------";
	QSqlQuery query(m_BooksDatabase);
	query.exec(QString("SELECT ISBN , TITLE, AUTHOR, PRESS, DATE, IMAGE FROM BOOKS_INFORMATION WHERE ISBN = '%1' OR TITLE = '%2' OR AUTHOR = '%3';")
		.arg(ISBN).arg(title).arg(author));

	while (query.next()) {
		QString sql_isbn = query.value("ISBN").toString();
		QString sql_title = query.value("TITLE").toString();
		QString sql_author = query.value("AUTHOR").toString();
		QString sql_press = query.value("PRESS").toString();
		QString sql_date = query.value("DATE").toString();
		QString sql_image = query.value("IMAGE").toString();

		qDebug() << "ISBN:" << sql_isbn << ", Title:" << sql_title
			<< ", Author:" << sql_author << ", Press:" << sql_press << ", Date:" << sql_date
			<< ", Image:" << sql_image;
		uBooksInfoList.push_back(UBooksInfo(sql_isbn, sql_title, sql_author, sql_press, sql_date, sql_image));
	}

	if(!uBooksInfoList.isEmpty())
	{
		Json::Value booksArray(Json::arrayValue);
		for (const auto& bookData : uBooksInfoList) {
			Json::Value book;
			book["isbn"] = bookData.m_isbn.toStdString();
			book["title"] = bookData.m_title.toStdString();
			book["author"] = bookData.m_author.toStdString();
			book["press"] = bookData.m_press.toStdString();
			book["date"] = bookData.m_date.toStdString();
			book["image"] = bookData.m_image.toStdString();
			booksArray.append(book);
		}
		Json::StreamWriterBuilder writer;
		std::string jsonString = Json::writeString(writer, booksArray);
		return QString::fromStdString(jsonString);
	}
	else
		return NULL;
}

bool UBooksDatabase::deleteBook(const QString& ISBN)
{
	QSqlQuery query(m_BooksDatabase);

	if (query.exec(QString("DELETE FROM BOOKS_INFORMATION WHERE ISBN='%1'").arg(ISBN)))
	{
		return true;
	}
	else
		return false;
}

void UBooksDatabase::changeBook(const QString& ISBN, const QString& title, const QString& author, const QString& press, const QString& date, const QString& initisbn)
{
	QSqlQuery query(m_BooksDatabase);
	query.exec(QString("UPDATE BOOKS_INFORMATION SET TITLE = '%2',AUTHOR = '%3',PRESS = '%4',DATE = '%5', ISBN = '%1' WHERE ISBN = '%6'").arg(ISBN)
		.arg(title)
		.arg(author)
		.arg(press)
		.arg(date)
		.arg(initisbn));
}
